var app = getApp();
var https = "https://www.moreclub.cn/";
var htp = 'http://10.150.7.245:8005/';
var util = require('../../utils/util.js');
var inputValue;
var page = 0;
var city;
var chcity;
var value;
var lat1;
var lng1;

var getList = function list(that,city) {
  var uid = wx.getStorageSync('infofrominput');
  var access_token = wx.getStorageSync('inToken');
  if (value != '') {
    //var that = this;
    wx.request({
      url: https + 'v1/merchant/open/fullsearch?query=' + value,
      data: {
        pn: page,
        ps: 30,
        //city: chcity
        city:city
      },
      success: function (res) {
        // success
        // var respData = res.data;
        // console.log(respData)

        // that.setData({
        //   respData: respData.data
        // })

        var listdata = res.data;
        console.log(res)
        console.log(listdata)
        if (listdata.success) {
          var list = listdata.data;
          var respData = [];//创建一个新数组
          for (var key in list) {
            var oldobj = list[key].tags;
            var tags = oldobj.split(",");//标签
            if (tags != null && tags.length > 3) {
              tags = tags.slice(0, 3);
            }

            var busName = list[key].busName;//商圈
            var sellingPoint = list[key].sellingPoint;//简介
            var name = list[key].name;//酒吧名
            var mid = list[key].mid;//商家id
            var thumb = list[key].thumb//图片
            var lat = list[key].lat;
            var lng = list[key].lng;
            var distance = util.distanceByLnglat(lng, lat, lng1, lat1);
            var s = (distance / 1000).toFixed(1);
            //console.log(distance / 1000 + 'km');

            var obj = {};//创建新对象
            obj.tags = tags;
            obj.busName = busName;
            obj.sellingPoint = sellingPoint;
            obj.name = name;
            obj.mid = mid;
            obj.thumb = thumb;
            obj.lat = lat;
            obj.lng = lng;
            obj.s = s;
            respData.push(obj);
          };
          //var newlist=alllist.slice(0,5)    
          if (respData != null) {
            //that.data.totallist.concat(respData);

            var oldlist = that.data.totallist;
            for (var i = 0; i < respData.length; i++) {
              oldlist.push(respData[i])
            }
          }

          that.setData({
            respData: respData,
            totallist: oldlist

          })

        }

      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
}


Page({
  data: {
    totallist: []
  },

  bindInput: function (e) {
    this.setData({
      inputValue: e.detail.value
    })
    value = this.data.inputValue
    // console.log(this.data.inputValue)
  },

  onShow:function(){
    city = wx.getStorageSync('city')

    lat1 = wx.getStorageSync('lat1');
    lng1 = wx.getStorageSync('lng1');
    if (city == '' || !city || typeof (city) == "undefined") {
      city = 'cd'
    }
    if (lat1 == '' || !lat1 || typeof (lat1) == "undefined") {
      lat1 = '30.632938'
    }
    if (lng1 == '' || !lng1 || typeof (lng1) == "undefined") {
      lng1 = '104.054685'
    }
  },

  onLoad: function () {
    var that = this;

   
  },

  btn: function () {
    var that = this;
    var arr = that.data.totallist;
    arr.splice(0)
    getList(that,city)

  },

  //  onReachBottom: function () {
  //    // 页面上拉触底事件的处理函数
  //    var that = this;
  //    if (that.data.respData != null && that.data.respData.length != 0) {
  //      page += 1;
  //      getList(that);
  //    } else{
  //      wx.showToast({
  //        title: '已加载完成',
  //        duration: 1000
  //      })
  //    }
  //  }
})